package com.telit.firecommand.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.util.ObjectUtil;
import com.telit.common.exception.ForestBaseException;
import com.telit.common.util.JwtTokenUtils;
import com.telit.firecommand.dao.CommFireAlarmDao;
import com.telit.firecommand.dao.CommFireHandleDao;
import com.telit.firecommand.dao.CommHistoryFireDao;
import com.telit.firecommand.entity.CommFireHandle;
import com.telit.firecommand.entity.CommHistoryFire;
import com.telit.firecommand.service.HistoryFireService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @author zhouyu
 * @description 灾损统计实现类
 */
@Service
@Transactional(rollbackFor = Exception.class)
@Slf4j
public class HistoryFireServiceImpl implements HistoryFireService {
    @Autowired
    private CommHistoryFireDao commHistoryFireDao;
    @Autowired
    private CommFireAlarmDao commFireAlarmDao;

    @Autowired
    private CommFireHandleDao commFireHandleDao;

    @Override
    public void addFireLoss(CommHistoryFire commHistoryFire) {
        Long fireId = commHistoryFire.getFireId();
        CommHistoryFire retCommHistroyFire = commHistoryFireDao.queryByFireId(fireId);
        if(retCommHistroyFire == null){
            retCommHistroyFire = new CommHistoryFire();
        }
        BeanUtil.copyProperties(commHistoryFire,retCommHistroyFire, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        commHistoryFireDao.deleteByFireId(fireId);
        commFireHandleDao.deleteByFireId(fireId);
//        String fireLevel = commHistoryFire.getFireLevel();
        String fireLevel = retCommHistroyFire.getFireLevel();
//        commFireAlarmDao.updateFinalLevelAndEndTime(fireId, fireLevel, commHistoryFire.getEndTime());
        commFireAlarmDao.updateFinalLevelAndEndTime(fireId, fireLevel, retCommHistroyFire.getEndTime());
        //插入灾损统计数据
//        commHistoryFireDao.insertSelective(commHistoryFire);
        commHistoryFireDao.insertSelective(retCommHistroyFire);
        //添加火情处理信息
        CommFireHandle commFireHandle = new CommFireHandle();
       /* commFireHandle.setCommanderInChief(commHistoryFire.getCommanderInChief());
        commFireHandle.setCommanderInChiefDuty(commHistoryFire.getCommanderInChiefDuty());
        commFireHandle.setFrontlineCommand(commHistoryFire.getFrontlineCommand());
        commFireHandle.setFrontlineCommandDuty(commHistoryFire.getFrontlineCommandDuty());
        commFireHandle.setRearCommand(commHistoryFire.getRearCommand());
        commFireHandle.setRearCommandDuty(commHistoryFire.getRearCommandDuty());*/
        commFireHandle.setCommanderInChief(retCommHistroyFire.getCommanderInChief());
        commFireHandle.setCommanderInChiefDuty(retCommHistroyFire.getCommanderInChiefDuty());
        commFireHandle.setFrontlineCommand(retCommHistroyFire.getFrontlineCommand());
        commFireHandle.setFrontlineCommandDuty(retCommHistroyFire.getFrontlineCommandDuty());
        commFireHandle.setRearCommand(retCommHistroyFire.getRearCommand());
        commFireHandle.setRearCommandDuty(retCommHistroyFire.getRearCommandDuty());
        commFireHandle.setFireId(fireId);
        String name = JwtTokenUtils.getRealName();
        commFireHandle.setCreateTime(new Date());
        commFireHandle.setCreateUser(name);
        commFireHandleDao.insert(commFireHandle);
    }

    @Override
    public void deleteById(Long id) {
        commHistoryFireDao.deleteByPrimaryKey(id);
    }

    @Override
    public int updateById(CommHistoryFire commHistoryFire) {
        //修改火灾等级
        Long fireId = commHistoryFire.getFireId();
        String fireLevel = commHistoryFire.getFireLevel();
        commFireAlarmDao.updateFinalLevelAndEndTime(fireId, fireLevel, commHistoryFire.getEndTime());
        return commHistoryFireDao.updateByPrimaryKeySelective(commHistoryFire);
    }

    @Override
    public CommHistoryFire queryById(Long id) {
        String finalLevel = commFireAlarmDao.queryFinalLevel(id);
        CommHistoryFire commHistoryFire = commHistoryFireDao.queryByFireId(id);
        if (ObjectUtil.isEmpty(commHistoryFire)) {
            commHistoryFire = new CommHistoryFire();
        }
        commHistoryFire.setFireLevel(finalLevel);
        //注释掉以下代码，因为当历史火情为null时，火情级别设置不了
        /*if (commHistoryFire != null && finalLevel != null) {
            commHistoryFire.setFireLevel(finalLevel);
        }*/
        return commHistoryFire;
    }

    @Override
    public void saveFirePoint(Long fireId, String firePointList) {
        // 判断该火情灾损统计是否存在
        CommHistoryFire commHistoryFire = queryById(fireId);
        if (commHistoryFire == null) {
            throw new ForestBaseException("该火情灾损数据不存在,请先录入");
        }
        if (updateById(new CommHistoryFire(commHistoryFire.getId(), firePointList)) <= 0) {
            if (updateById(commHistoryFire) <= 0) {
                log.error("火烧迹线数据存库失败,fireId:{}", fireId);
                throw new ForestBaseException("火烧迹线数据存储失败");
            }
        }
    }

}
